package com.endurance.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/ribbon01")
public class Ribbon1Controller {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping
    public HashMap ribbon() {
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("PRODUCT");
        serviceInstances.forEach(serviceInstance -> {
            System.out.println("当前服务uri:" + serviceInstance.getUri());
            System.out.println("当前服务主机:" + serviceInstance.getHost());
            System.out.println("当前服务端口:" + serviceInstance.getPort());
        });
        HashMap forObject = new RestTemplate().getForObject(serviceInstances.get(0).getUri() + "/product/findAll", HashMap.class);
        System.out.println(forObject);
        return forObject;
    }


}
